<template>
  <div class="app-container">
    <div class="searchform">
      <ToolBar :filters="filters" :getList="getList" :handleAdd="handleCreate"/>
    </div>
    <el-col :span="12" class="customerName" style="padding-bottom: 0px; float:right">
      <span>所属公司：</span>
      {{customerName}}
    </el-col>
    <el-table v-loading="listLoading" :data="memberList" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="ID" width="95" type="index"></el-table-column>
      <el-table-column label="姓名" align="center" width="100">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="职务" align="center" width="200">
        <template slot-scope="scope">{{ scope.row.post }}</template>
      </el-table-column>
      <el-table-column label="人员" align="center" prop="job"  width="120">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.job == '组长'">组长</el-tag>
          <el-tag v-else-if="scope.row.job == '副组长'">副组长</el-tag>
          <el-tag v-else type="info">组员</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="duty"  label="职责" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="200" width="200" align="center">
        <template slot-scope="scope">
          <el-button size="small" type="warning" @click="handleUpdate(scope.$index, scope.row)">
            <i class="el-icon-edit"></i>
          </el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDel(scope.$index, scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--翻页-->
    <Pagination v-show="total>10" :total="total" :pageSize="pagesize" :handleCurrentChange="handleCurrentChange"/>
    <!--编辑界面-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form label-width="100px" :model="Form" :rules="FormRules" ref="dataForm">
        <el-form-item label="人员" prop="job" width="100%">
          <el-select v-model="Form.job" placeholder="选择项目类型">
            <el-option v-for="item in jobCodeOptions" :key="item.id" :label="item.job" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name" width="100%">
          <el-input v-model="Form.name"></el-input>
        </el-form-item>
        <el-form-item label="职责" prop="duty" width="100%">
          <el-input v-model="Form.duty"></el-input>
        </el-form-item>
        <el-form-item label="职务" prop="post" width="100%">
          <el-input v-model="Form.post"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getMembersList,
  createMember,
  updateMember,
  deleteMember,
  jobCode
} from "@/api/customerMembers";
import { getCustomerList } from "@/api/customers";
import ToolBar from "@/views/layout/components/ToolBar";
import Pagination from "@/views/layout/components/Pagination";
import { isvalidPhone } from "@/utils/validate";
const validPhone = (rule, value, callback) => {
  if (!value) {
    callback(new Error("请输入电话号码"));
  } else if (!isvalidPhone(value)) {
    callback(new Error("请输入正确的11位手机号码"));
  } else {
    callback();
  }
};
export default {
  components: { ToolBar, Pagination },
  data() {
    return {
      customerName: "",
      filters: {
        name: "",
        placeholder: "成员姓名"
      },
      jobCodeOptions: [],
      page: 1,
      total: 0,
      pagesize: 10,
      memberList: [],
      listLoading: false,

      dialogStatus: "",
      textMap: {
        update: "编辑成员",
        create: "新增成员"
      },
      dialogFormVisible: false,
      Form: {
        job: "",
        name: "",
        duty: "",
        post: ""
      },
      FormRules: {
        job: [{ required: true, message: "请选择人员职位", trigger: "change" }],
        name: [{ required: true, message: "请输入人员名称", trigger: "blur" }],
        duty: [{ required: true, message: "请输入职责", trigger: "blur" }],
        post: [{ required: true, message: "请输入职务",  trigger: "blur" }]
      },
      id: this.$route.params.id
    };
  },
  created() {
    this.getList();
    this.getJobCode();
    this.getCustomer();
  },
  methods: {
    //获取所属公司
    getCustomer() {
      getCustomerList().then(response => {
        response.data.records.forEach(element => {
          if (element.id == this.id) {
            this.customerName = element.customerName;
          }
        });
      });
    },
    // 获取职位
    getJobCode() {
      jobCode().then(response => {
        this.jobCodeOptions = response.data;
        });
    },
    // 获取列表
    getList() {
      this.listLoading = true;
      getMembersList({
        customerid: this.id,
        name: this.filters.name,
        page: this.page,
        size: this.pagesize
      }).then(response => {
        console.log(response)
        this.memberList = response.data.records;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    // 翻页
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },
    //重置form
    resetForm() {
      this.Form = {
        job: "",
        name: "",
        duty: "",
        post: ""
      };
    },
    // 显示增加界面
    handleCreate() {
      this.resetForm();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          let tempData = {
            customerId: this.id
          };
          Object.assign(tempData, this.Form);
          createMember(tempData).then(() => {
            this.getList();
            this.dialogFormVisible = false;
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
              duration: 2000
            });
          });
        }
      });
    },
    //显示编辑界面
    handleUpdate(index, row) {
      this.Form = Object.assign(this.Form, row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
    },
    //编辑数据
    updateData() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.jobCodeOptions.map(item=>{
            if(this.Form.job == item.job){
              return this.Form.job = item.id
            }
          })
          const tempData = Object.assign({}, this.Form);
          updateMember(tempData).then(() => {
            this.getList();
            this.dialogFormVisible = false;
            this.$notify({
              title: "成功",
              message: "更新成功",
              type: "success",
              duration: 2000
            });
          });
        }
      });
    },
    //删除数据
    handleDel(index, row) {
      this.$confirm("确认删除该客户成员吗?", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning"
      }).then(() => {
        this.listLoading = true;
        deleteMember({ id: row.id }).then(() => {
          this.listLoading = false;
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          const index = this.memberList.indexOf(row);
          this.memberList.splice(index, 1);
        });
      }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });;
    }
  }
};
</script>
<style lang="scss" scoped>
.searchform {
  float: left;
  //display: inline;
}
.customerName {
  font-size: 14px;
  text-align: right;
  span {
    font-weight: 700;
    display: inline-block;
    line-height: 40px;
  }
}
</style>


